package com.yh.csx.crm.task;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.csx.crm.core.enums.SapSyncTypeEnum;
import com.yh.csx.crm.core.exception.BusinessException;
import com.yh.csx.crm.core.util.DateUtils;
import com.yh.csx.crm.core.util.SapUtils;
import com.yh.csx.crm.dao.CustomerMapper;
import com.yh.csx.crm.dao.CustomerSyncLogMapper;
import com.yh.csx.crm.po.Customer;
import com.yh.csx.crm.po.CustomerSyncLog;
import com.yh.csx.crm.service.vo.SAPResponse;
import com.yh.csx.crm.service.vo.SapCustomer;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * SapSchedule class
 *
 * @author huyalin
 * @date 2019/3/11
 */
@Slf4j
@Component
public class SapScheduleTask {

    private static ExecutorService thread = Executors.newCachedThreadPool();
    private static ExecutorService task = Executors.newFixedThreadPool(5);

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    CustomerSyncLogMapper customerSyncLogMapper;


    @Scheduled(cron = "0 0 0 * * ?")
    public void task() {
        log.info("\n******************************************开始执行批量***********************************************");
        for (int i = 1; ; i++) {
            Page<Object> page = PageHelper.startPage(i, 100);
            List<Customer> customerList = customerMapper.selectAllSynchronized();
            customerList.forEach(customer -> {
                this.edit(customer, 1);
            });
            if (i >= page.getPages()) {
                break;
            }
        }
        log.info("\n******************************************结束执行批量***********************************************");
    }

    private void edit(Customer customer, Integer type) {

        thread.submit(() -> {
            try {

                CustomerSyncLog customerSyncLog = new CustomerSyncLog();
                customerSyncLog.setCustomerId(customer.getId());
                customerSyncLog.setCustomerNumber(customer.getCustomerNumber());
                customerSyncLog.setSyncType(SapSyncTypeEnum.EDIT.getCode());
                customerSyncLog.setRemark("每日额度同步");
                customerSyncLogMapper.insertSelective(customerSyncLog);

                SapCustomer data = new SapCustomer().init();
                val header = data.getSheet().get(0).getHeader();
                var item = data.getSheet().get(0).getItem().get(0);

                header.put("type", type+"");//修改类型
                header.put("source", "CX");//数据来源(固定值)
                header.put("customer_number", customer.getCustomerNumber());//客户编号
                header.put("customer_name", customer.getCustomerName());//客户名称
                header.put("phone", customer.getPhone());//电话号码
                header.put("invoice_title", customer.getInvoiceName());//开票名称
                header.put("social_credit_code", customer.getSocialCreditCode());//增值税登记号
                header.put("region_province", customer.getRegionProvince());//省
                header.put("region_city", customer.getRegionCity());//市
                header.put("channel_second_code", customer.getFirstCategoryCode() + "");//二级分销渠道
                header.put("category_code1", customer.getSecondCategoryCode());//一级客户性质
                header.put("customer_code2", customer.getThirdCategoryCode());//二级客户性质
                header.put("sales_province_id", customer.getSalesProvince() + "");//所属省区
                header.put("company_code", customer.getCompanyCode() + "");//公司代码
                header.put("payment_terms", customer.getPaymentTerms() + "");//付款条件
                header.put("sale_organization", customer.getCompanyCode());//销售组织
                header.put("credit_range", customer.getCreditRange());//信控范围
                header.put("customer_level", customer.getCustomerLevel() + ""); //信控级别
//                header.put("credit_limit", customer.getCreditLimit() + "");//信控额度
                if (Objects.nonNull(customer.getTempCreditLimit()) && customer.getTempCreditLimit().doubleValue() > 0 &&
                        DateUtils.belongCalendar(new Date(), customer.getTempBeginTime(), customer.getTempEndTime())) {
                    header.put("credit_limit", customer.getTempCreditLimit().add(customer.getCreditLimit()) + "");//信控额度
                } else {
                    header.put("credit_limit", customer.getCreditLimit() + "");//信控额度
                }
                header.put("distribution_channel", customer.getDistributionChannel());//分销渠道
                header.put("sale_region", customer.getSaleRegion());//销售地区
                item.put("name", customer.getCustomerName());//联系人名称1
                item.put("phone1", customer.getPhone());//联系人电话
                item.put("bank_account_number", customer.getBankAccountNumber());//银行账户
                item.put("bank_account_name", customer.getBankAccountName());//银行户主
                item.put("bank_number", customer.getBankNumber());//联行号


                // 将SAP请求参数 先行存入日志
                String url = SapUtils.sap_update_customer_url;      // SAP请求地址
                String request_str = JSON.toJSONString(data);       // SAP请求参数
                String userName =  SapUtils.sap_customer_user;      // SAP请求用户名
                String password = SapUtils.sap_customer_password;   // SAP请求密码

                StringBuilder sb = new StringBuilder();
                sb.append("url:{").append(url).append("},")
                        .append("name:{").append(userName).append("},")
                        .append("pwd:{").append(password).append("},")
                        .append("param:{").append(request_str).append("},")
                        .append("hear:{").append(header).append("}");
                String requestContent = sb.toString();
                customerSyncLog.setSapRequest(requestContent);
                customerSyncLogMapper.updateByPrimaryKeySelective(customerSyncLog); // 存入请求日志
                var response = SapUtils.sendHttpPostAuth(SapUtils.sap_update_customer_url, JSON.toJSONString(data), SapUtils.sap_customer_user, SapUtils.sap_customer_password, null);
                log.info("彩食鲜团购传ECC接口update-返回结果：{}",response);

                Optional.ofNullable(response).orElseThrow(() ->  new BusinessException(response));
                if (!StringUtils.isEmpty(response)) {
//                    val j = JSON.parseObject(response, new TypeReference<HashMap<String, Object>>() {});
                    val j = JSON.parseObject(response, SAPResponse.class);
                    if (j.getITEM().get(0).getFD_SYNC_STATUS().equals("0")) {
                        customerSyncLog.setStatus(true);
                    }
                    val sapId = j.getKUNNR(); // 得到客户号ID
                    customerSyncLog.setCustomerNumber(sapId);
                    customerSyncLog.setSapResponse(response);
                    customerSyncLogMapper.updateByPrimaryKeySelective(customerSyncLog);
                }

            } catch (Exception exp) {
                log.error("编辑同步sap客户信息异常，customerId:"+customer,exp);
            }
        });
    }
}
